//
//  ZDModalAnimation.swift
//  1026SwiftSinaWeiBo
//
//  Created by Apple on 15/11/9.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

class ZDModalAnimation: NSObject, UIViewControllerAnimatedTransitioning {

    
    ///动画时间
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        
        return 0.5
    }
    
    ///转场动画
    //实现该方法 需要手动添加执行动画的view到容器中  
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
        //拿到执行动画的view
        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
        
        //添加到容器view中
        transitionContext.containerView()?.addSubview(toView)
        
        //设置初始transform
        toView.transform = CGAffineTransformMakeScale(1.0, 0)
        
        //修改锚点  ———— 默认锚点是中心点(0.5, 0.5) 动画参考锚点执行
        toView.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
        
        
        UIView.animateWithDuration(transitionDuration(nil), delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 2, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
//            view.transform = CGAffineTransformMakeScale(1.0, 1.0)
            
            toView.transform = CGAffineTransformIdentity
            
            }) { (_) -> Void in
                
                //告诉系统转场动画执行完毕  ———— 必须要有，否则系统会认为转场动画没有完毕，不能进行用户交互
                transitionContext.completeTransition(true)
        }
    }
    
}
